<template>
<div>
    <div class="search">
        <div class="header-input">
        <input v-model="keyword" type="text" class="search-input" placeholder="输入城市名或拼音">
        </div>
    </div>
    <div class="search-content" ref="search" v-show="keyword">
        <ul>
            <li class="search-item border-bottom" v-for="(item,index) of list" :key="index">{{item.name}}</li>
            <li class="search-item-null" v-show="hasList">没有找到匹配内容</li>
        </ul>
    </div>
</div>

</template>

<script>
// eslint-disable-next-line no-unused-vars
import Bscroll from 'better-scroll'
export default {
  name: 'CitySearch',
  data () {
    return {
      keyword: '',
      list: [],
      timer: null
    }
  },
  props: {
    cities: Object
  },
  computed: {
    hasList () {
      return !this.list.length
    }
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.search)
  },
  watch: {
    keyword () {
      if (this.timer) {
        clearTimeout(this.timer)
      }
      if (!this.keyword) {
        this.list = []
        return
      }
      this.timer = setTimeout(() => {
        // eslint-disable-next-line no-unused-vars
        const result = []
        for (let i in this.cities) {
          this.cities[i].forEach((value) => {
            //   如果有内容就往搜索的内容里面加
            if (value.spell.indexOf(this.keyword) > -1 ||
            value.name.indexOf(this.keyword) > -1) {
              result.push(value)
            }
          })
        }
        this.list = result
      }, 100)
    }
  }
}
</script>
<style lang="stylus" scoped>
@import '~styles/varibles.styl'
.border-bottom
    &:before
        border-color #ccc
.list
.header-input
    height :.72rem
    padding 0 .1rem
    background :$backgroundColor
    .search-input
        box-sizing border-box  //边框留白
        padding 0 .1rem //边框留白
        width :100%
        height :.62rem
        text-align center
        background #fff
        border-radius :.05rem
.search-content
    //设置显示层级
    z-index 1
    overflow hidden
    position absolute
    top : 1.58rem
    left 0
    right 0
    bottom 0
    background #eee
    .search-item
        color #666
        line-height .62rem
        background #fff
        padding-left .2rem
    .search-item-null
        color #666
        line-height .82rem
        padding-left .0rem
        text-align center
        font-size .30rem
</style>
